package cn.jbolt.core.service;

import cn.jbolt.core.base.config.JBoltConfig;
import cn.jbolt.core.bean.JBoltDateRange;
import cn.jbolt.core.common.enums.JBoltSystemLogOpenType;
import cn.jbolt.core.common.enums.JBoltSystemLogTargetType;
import cn.jbolt.core.common.enums.JBoltSystemLogType;
import cn.jbolt.core.db.sql.Sql;
import cn.jbolt.core.enumutil.JBoltEnum;
import cn.jbolt.core.model.SystemLog;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.core.util.JBoltDateUtil;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;

public class JBoltSystemLogService extends JBoltBaseService<SystemLog> {
	protected SystemLog dao = new SystemLog().dao();
	@Override
	protected SystemLog dao() {
		return dao;
	}

	/**
	 * 根据操作类型获取操作名称
	 *
	 * @param type
	 * @return
	 */
	public String typeName(int type) {
		String name = JBoltEnum.getTextByValue(JBoltSystemLogType.class, type);
		if (notOk(name)) {
			name = typeToName(type);
			if (notOk(name)) {
				name = "未指定";
			}
		}
		return name;
	}

	/**
	 * type 转 name
	 *
	 * @param type
	 * @return
	 */
	public String typeToName(int type) {
		JBoltProjectSystemLogProcessor projectSystemLogProcessor = JBoltConfig.getProjectSystemLogProcessor();
		if(projectSystemLogProcessor == null) {return null;}
		return projectSystemLogProcessor.typeToName(type);
	}

	/**
	 * targetType 转 name
	 *
	 * @param targetType
	 * @return
	 */
	public String targetTypeToName(int targetType) {
		JBoltProjectSystemLogProcessor projectSystemLogProcessor = JBoltConfig.getProjectSystemLogProcessor();
		if(projectSystemLogProcessor == null) {return null;}
		return projectSystemLogProcessor.targetTypeToName(targetType);
	}

	/**
	 * 根据操作目标类型获取操作目标名称
	 *
	 * @param targetType
	 * @return
	 */
	public String targetTypeName(int targetType) {
		String name = JBoltEnum.getTextByValue(JBoltSystemLogTargetType.class, targetType);
		if (notOk(name)) {
			name = targetTypeToName(targetType);
			if (notOk(name)) {
				name = "未指定";
			}
		}
		return name;

	}

	/**
	 * 分页查询
	 *
	 * @param pageNumber
	 * @param pageSize
	 * @param keywords
	 * @param dateRange
	 * @return
	 */
	public Page<SystemLog> paginateAdminList(int pageNumber, Integer pageSize, String keywords, JBoltDateRange dateRange) {
		Sql sql = selectSql().page(pageNumber,pageSize).orderById(true);
		sql.like("title",keywords);
		sql.betweenDateRange("create_time",dateRange);
		return paginate(sql);
	}

	/**
	 * 添加系统日志
	 *
	 * @param type           操作类型
	 * @param targetType     关联目标类型
	 * @param targetId       关联目标ID
	 * @param title          标题内容
	 * @param dictionaryType 关联字典数据类型
	 * @param userId         操作人ID
	 * @param userName       操作人name
	 */
	public void saveLog(int type, int targetType, Object targetId, String title, int dictionaryType, Object userId,
			String userName) {
		SystemLog log = new SystemLog();
		log.set("user_id", userId == null ? 0L : userId);
		log.setCreateTime(JBoltDateUtil.getNow());
		log.set("target_id", targetId);
		log.setTargetType(targetType);
		log.setType(type);
		log.setUserName(userName);
		log.setTitle(title);
		log.setOpenType(JBoltSystemLogOpenType.URL.getValue());
		if (type != JBoltSystemLogType.DELETE.getValue()) {
			String url = processJBoltCoreLogUrl(targetType, targetId);
			if(notOk(url)) {
				url = processSystemLogUrl(targetType, targetId);
			}
			log.setUrl(url);
		}
		log.save();
	}

	/**
	 * 自行扩展实现 获得log url
	 * @param targetType
	 * @param targetId
	 * @return
	 */
	protected String processSystemLogUrl(int targetType, Object targetId) {
		JBoltProjectSystemLogProcessor projectSystemLogProcessor = JBoltConfig.getProjectSystemLogProcessor();
		if(projectSystemLogProcessor == null) {return null;}
		return projectSystemLogProcessor.processSystemLogUrl(targetType,targetId);
	}

	/**
	 * 内部core实现 获得log url
	 * @param targetType
	 * @param targetId
	 * @return
	 */
	private String processJBoltCoreLogUrl(int targetType, Object targetId) {
		JBoltSystemLogTargetType targetTypeEnum = JBoltEnum.getEnumObjectByValue(JBoltSystemLogTargetType.class, targetType);
		if(targetTypeEnum == null) {return null;}
		String url = null;
		switch (targetTypeEnum) {
		case DICTIONARY_TYPE:
			url = "admin/dictionary/show/" + targetId;
			break;
		case DICTIONARY:
			url = "admin/dictionary/show/" + targetId;
			break;
		case PERMISSION:
			url = "admin/permission/show/" + targetId;
			break;
		case ROLE:
			url = "admin/role/show/" + targetId;
			break;
		case USER:
			url = "admin/user/show/" + targetId;
			break;
		case GLOBAL_CONFIG:
			url = "admin/globalconfig/show/" + targetId;
			break;
		case GLOBAL_CONFIG_TYPE:
			url = "admin/globalconfig/type/show/" + targetId;
			break;
		case DEPT:
			url = "admin/admin/dept/show/" + targetId;
			break;
		case POST:
			url = "admin/admin/post/show/" + targetId;
			break;
		default:
			break;
		}
		return url;
	}

	@Override
	protected int systemLogTargetType() {
		return JBoltSystemLogTargetType.NONE.getValue();
	}

}
